package com.flyme.module.base.provider.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.security.OpenHelper;
import com.flyme.common.core.security.OpenUserAuth;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.common.core.utils.StringUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.SysDept;
import com.flyme.module.base.client.entity.SysUser;
import com.flyme.module.base.provider.mapper.SysDeptMapper;
import com.flyme.module.base.provider.service.SysDeptService;
import com.flyme.module.base.provider.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author: flyme
 * @date: 2018/3/7 15:27
 * @desc: 部门接口实现
 */
@Service
public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {

    @Autowired
    private SysUserService userService;

    /**
     * 根据企业查询部门
     */
    public List<Map<String, Object>> selectDept(String companyId, String parentId) {
        return listMaps(q().select("id value,deptName label").eq("companyId", companyId).eq("parentId", parentId));
    }

    @Override
    public Boolean add(SysDept dept) {
        OpenUserAuth user = OpenHelper.getUserAuth();
        String parentId = dept.getParentId();
        dept.setParentId(StringUtils.isBlank(parentId) ? "0" : parentId);
        dept.setSortOrder(ObjectUtils.isEmpty(dept.getSortOrder()) ? 0 : dept.getSortOrder());
        dept.setIsParent(0);
        dept.setCompanyId(user.getCompanyId());
        Long headerId = dept.getHeaderId();
        if (ObjectUtils.isNotEmpty(headerId)) {
            UpdateWrapper u = new UpdateWrapper<SysUser>();
            userService.update(u.set(true, "deptName", dept.getDeptName()).set(true, "deptId", dept.getId()).eq(true, "id", headerId));
        }
        if (!parentId.equals("0")) {
            SysDept parent = getById(parentId);
            parent.setIsParent(1);
            //更新父节点
            saveOrUpdate(parent);
        }
        return save(dept);
    }

    @Override
    public Boolean edit(SysDept dept) {
        Long headerId = dept.getHeaderId();
        if (ObjectUtils.isNotEmpty(headerId)) {
            UpdateWrapper u = new UpdateWrapper<SysUser>();
            userService.update(u.set(true, "deptName", dept.getDeptName()).set(true, "deptId", dept.getId()).eq(true, "id", headerId));
        }
        return saveOrUpdate(dept);
    }

    @Override
    public ResultBody delById(Long deptId) {
        SysDept dept = getById(deptId);
        String parentId = dept.getParentId();
        if (dept.getIsParent().equals(1)) {
            int n = count(q().eq("parentId", deptId));
            if (n > 0) {
                return ResultBody.failed("请先删除子部门");
            }
        } else {
            int n = count(q().eq("parentId", parentId));
            if (n == 1) {
                UpdateWrapper u = new UpdateWrapper();
                update(u.set(true, "isParent", 0).eq(true, "id", parentId));
            }
        }
        removeById(deptId);
        return ResultBody.success("刪除成功");
    }

    @Override
    public List<Long> getDeptId(Long headerId) {
        return listObjs(q().select("id").eq("headerId", headerId), e -> Long.parseLong(e.toString()));
    }

    /**
     * 根据id查询部门名称
     */
    public List<String> selectByIds(String[] ids){
        return listObjs(q().select("deptName").in("id", ids), e -> e.toString());
    }

    /**
     * 根据hr查询部门名称
     */
    public String getDeptName(Long headerId) {
        List<String> list = listObjs(q().select("deptName").eq("headerId", headerId), e -> e.toString());
        String deptName = "";
        for(String dept : list){
            deptName = deptName + "," + dept;
        }
        if(ObjectUtils.isNotEmpty(deptName)){
            return deptName.substring(1);
        }
        return deptName;
    }

    @Override
    public EntityMap selectDept(){
        EntityMap map = new EntityMap();
        List<SysDept> list = list();
        for(SysDept dept : list){
            map.put(dept.getDeptName(), dept.getId());
        }
        return map;
    }

}
